<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>xiaxuchen的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="vue + springmvc前后端传递数组参数问题spring mvc获取数组参数的两种方式实现要求：请求需要获取一个ids的Integer类型的数组  方式一 请求中的请求参数以如下方式传递： ​    http:&#x2F;&#x2F;主机名:端口名&#x2F;路径?ids&#x3D;1&amp;ids&#x3D;2 springmvc服务器端不需要再参数前添加注解，直接使用Integer[]**或List&lt;Integer&gt;**">
<meta property="og:type" content="article">
<meta property="og:title" content="xiaxuchen的博客">
<meta property="og:url" content="http://yoursite.com/2020/08/07/%E5%90%8E%E7%AB%AF/vue%20+%20springmvc%E5%89%8D%E5%90%8E%E7%AB%AF%E4%BC%A0%E9%80%92%E6%95%B0%E7%BB%84%E5%8F%82%E6%95%B0%E9%97%AE%E9%A2%98/index.html">
<meta property="og:site_name" content="xiaxuchen的博客">
<meta property="og:description" content="vue + springmvc前后端传递数组参数问题spring mvc获取数组参数的两种方式实现要求：请求需要获取一个ids的Integer类型的数组  方式一 请求中的请求参数以如下方式传递： ​    http:&#x2F;&#x2F;主机名:端口名&#x2F;路径?ids&#x3D;1&amp;ids&#x3D;2 springmvc服务器端不需要再参数前添加注解，直接使用Integer[]**或List&lt;Integer&gt;**">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-08-07T15:08:02.073Z">
<meta property="article:modified_time" content="2020-07-19T10:36:54.104Z">
<meta property="article:author" content="xiaxuchen">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="./atom.xml" title="xiaxuchen的博客" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="./css/style.css">

<meta name="generator" content="Hexo 5.0.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="./" id="logo">xiaxuchen的博客</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="./">Home</a>
        
          <a class="main-nav-link" href="./archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="./atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-后端/vue + springmvc前后端传递数组参数问题" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="./2020/08/07/%E5%90%8E%E7%AB%AF/vue%20+%20springmvc%E5%89%8D%E5%90%8E%E7%AB%AF%E4%BC%A0%E9%80%92%E6%95%B0%E7%BB%84%E5%8F%82%E6%95%B0%E9%97%AE%E9%A2%98/" class="article-date">
  <time datetime="2020-08-07T15:08:02.073Z" itemprop="datePublished">2020-08-07</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="vue-springmvc前后端传递数组参数问题"><a href="#vue-springmvc前后端传递数组参数问题" class="headerlink" title="vue + springmvc前后端传递数组参数问题"></a>vue + springmvc前后端传递数组参数问题</h1><h2 id="spring-mvc获取数组参数的两种方式"><a href="#spring-mvc获取数组参数的两种方式" class="headerlink" title="spring mvc获取数组参数的两种方式"></a>spring mvc获取数组参数的两种方式</h2><p>实现要求：请求需要获取一个ids的Integer类型的数组</p>
<ol>
<li><p>方式一</p>
<p>请求中的请求参数以如下方式传递：</p>
<p>​    <strong>http://主机名:端口名/路径?ids=1&amp;ids=2</strong></p>
<p>springmvc服务器端不需要再参数前添加注解，直接使用<strong>Integer[]**或</strong>List&lt;Integer&gt;**类型即可，代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/getIds&quot;)</span></span><br><span class="line"><span class="comment">// 下面的@RequestParam(&quot;ids&quot;)可以不要</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getIds</span><span class="params">(<span class="meta">@RequestParam(&quot;ids&quot;)</span> Integer[] ids)</span></span>&#123;&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>方式二</p>
<p>请求中的请求参数以如下方式传递(其中的**[]**需要进行URL编码)：</p>
<p>​    <strong>http://主机名:端口名/路径?ids[]=1&amp;ids[]=2</strong></p>
<p>在方式一中的参数前需要添加@RequestParam注解，并注明参数名称为**ids[]**，代码如下:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/getIds&quot;)</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getIds</span><span class="params">(<span class="meta">@RequestParam(&quot;ids[]&quot;)</span> String[] ids)</span></span>&#123;&#125;</span><br></pre></td></tr></table></figure>

</li>
</ol>
<h2 id="遇到的问题"><a href="#遇到的问题" class="headerlink" title="遇到的问题:"></a>遇到的问题:</h2><h3 id="服务器无法识别字符"><a href="#服务器无法识别字符" class="headerlink" title="服务器无法识别字符"></a>服务器无法识别字符</h3><ul>
<li><p>问题描述</p>
<blockquote>
<p>Invalid character found in the request target. The valid characters are …</p>
</blockquote>
<p>这个问题是我在使用axios发送delete请求时传输数组数据遇到的，原因是axios没有将数组的中括号**[]**编码，服务器端无法识别导致的</p>
</li>
<li><p>解决办法</p>
<p>使用qs，对参数进行转码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">const del &#x3D; function (apiPath, params, successCallback, errorCallback) &#123;</span><br><span class="line">  return http.delete(apiPath, &#123;</span><br><span class="line">    params: params,</span><br><span class="line">    &#x2F;&#x2F; 这个将对参数进行转码</span><br><span class="line">    paramsSerializer: params &#x3D;&gt; &#123;</span><br><span class="line">      return qs.stringify(params, &#123; indices: false &#125;)</span><br><span class="line">    &#125; &#125;)</span><br><span class="line">  &#x2F;&#x2F; thenResponse(req, successCallback, errorCallback)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



</li>
</ul>
<p>  以上代码封装了向服务器发送delete请求的操作</p>
<h3 id="axios只发送options请求"><a href="#axios只发送options请求" class="headerlink" title="axios只发送options请求"></a>axios只发送options请求</h3><ul>
<li><p>问题描述</p>
<p>由于跨域的预检机制，每次发送非简单请求的时候axios都会发送一次options请求查看是否有权限访问，但是我发送请求时发送完options请求后就不再发真正的请求了.</p>
</li>
<li><p>问题原因</p>
<p>我在使用axios.create方法时指定了<strong>withCredentials: true</strong></p>
<p>错误代码如下:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> http = axios.create(&#123;</span><br><span class="line">  baseURL: <span class="string">&#x27;http://localhost:8086/app&#x27;</span>,</span><br><span class="line">  <span class="comment">// 这里的代码导致了错误，删除即可</span></span><br><span class="line">  withCredentials: <span class="literal">true</span>,</span><br><span class="line">  transformRequest: [<span class="function"><span class="keyword">function</span> (<span class="params">data</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> newData = <span class="string">&#x27;&#x27;</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> k <span class="keyword">in</span> data) &#123;</span><br><span class="line">      <span class="keyword">if</span> (data.hasOwnProperty(k) === <span class="literal">true</span>) &#123;</span><br><span class="line">        newData += <span class="built_in">encodeURIComponent</span>(k) + <span class="string">&#x27;=&#x27;</span> + <span class="built_in">encodeURIComponent</span>(data[k]) + <span class="string">&#x27;&amp;&#x27;</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> newData</span><br><span class="line">  &#125;]</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>





</li>
</ul>
<h2 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h2><ul>
<li><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS">跨域中的预检机制MDN文档</a></li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/08/07/%E5%90%8E%E7%AB%AF/vue%20+%20springmvc%E5%89%8D%E5%90%8E%E7%AB%AF%E4%BC%A0%E9%80%92%E6%95%B0%E7%BB%84%E5%8F%82%E6%95%B0%E9%97%AE%E9%A2%98/" data-id="ckdkdagvc000ovoutd34b0ips" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="./2020/08/07/%E5%90%8E%E7%AB%AF/Vue-Router/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          (no title)
        
      </div>
    </a>
  
  
    <a href="./2020/08/07/%E5%90%8E%E7%AB%AF/SSM%E5%90%84%E9%85%8D%E7%BD%AE%E7%9A%84%E8%A7%A3%E6%9E%90/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title"></div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="./archives/2020/08/">August 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E9%93%BE%E8%A1%A8/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E6%95%B0%E7%BB%84/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%80%9D%E7%BB%B4%E5%AF%BC%E5%9B%BE/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/SpringBoot%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">(no title)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 xiaxuchen<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="./" class="mobile-nav-link">Home</a>
  
    <a href="./archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="./fancybox/jquery.fancybox.css">

  
<script src="./fancybox/jquery.fancybox.pack.js"></script>




<script src="./js/script.js"></script>




  </div>
</body>
</html>